package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.foolishbird.sdk.assist.Constant;
import com.foolishbird.sdk.core.HttpEngineApi;
import com.foolishbird.sdk.core.HttpEngineConduits;
import com.foolishbird.sdk.params.bean.PopupConfig;
import com.foolishbird.sdk.ui.assist.LoginBindPagerAdapter;
import com.foolishbird.sdk.ui.assist.OnFinishActivityListener;
import com.foolishbird.sdk.ui.view.ExViewPager;
import com.foolishbird.sdk.utils.WL;

import java.util.Arrays;


public class FoolishBirdBindDialogFragment extends BaseDialogFragment implements View.OnClickListener {

    private static final String TAG = FoolishBirdBindDialogFragment.class.getSimpleName();

//    private ImageButton mCloseWindowBtn;
//    private TextView mTitleTextView,mSubTitleTextView;
    private ExViewPager mExViewPager;

    private LoginBindPagerAdapter mPagerAdapter;

    private ForceRealNameCertifiesBroadcastReceiver mBroadcastReceiver;

    public static FoolishBirdBindDialogFragment newInstance(Bundle bundle) {
        FoolishBirdBindDialogFragment fragment = new FoolishBirdBindDialogFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isNullContext()) {
            mBroadcastReceiver = new ForceRealNameCertifiesBroadcastReceiver();
            IntentFilter filter = new IntentFilter(Constant.LocalIntentAction.INTENT_AACTION_FORCE_REALNAME_CERTIFIES);
//            LocalBroadcastManager.getInstance(getContext()).registerReceiver(mBroadcastReceiver, filter);
            getActivity().registerReceiver(mBroadcastReceiver, filter);
        } else {
            WL.i(TAG, "onCreate ========> Context is null ......");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!isNullContext()) {
            getActivity().unregisterReceiver(mBroadcastReceiver);
//            LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mBroadcastReceiver);
        } else {
            WL.i(TAG, "onDestroy ========> Context is null ......");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View contentView = doInflateContentView(inflater, container, getLayoutIdentifier("fb_fragment_bind"));

        mExViewPager = contentView.findViewById(getViewIdentifier("activity_bindphone_viewpager"));
//        mCloseWindowBtn = contentView.findViewById(getViewIdentifier("activity_bindphone_finish_btn"));
//        mCloseWindowBtn.setOnClickListener(this);

        mExViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position <= 0) {
//                    mTitleTextView.setText(getStringIdentifier("hnssn_bind_bingphone"));
//                    mSubTitleTextView.setText("");
                } else {
                    HttpEngineConduits wanSDKDataDao = HttpEngineApi.getInstance().getEngineConduits();
//                    String tip = wanSDKDataDao.readIdcardMsg();
//                    mSubTitleTextView.setText(TextUtils.isEmpty(tip)?"":("("+tip+")"));
//                    mTitleTextView.setText(getStringIdentifier("hnssn_bind_realname_authentication"));
//                    PopupConfig popupConfig = getPopupConfig();
//                    if (popupConfig == null || popupConfig.isIdcardPopupCloseEnable()) {
//                        mCloseWindowBtn.setVisibility(View.VISIBLE);
//                    } else {
//                        mCloseWindowBtn.setVisibility(View.GONE);
//                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

//        boolean withPhone = HttpEngineApi.getInstance().getEngineConduits().readIDcardUiType()==1;
        Fragment[] fragments;
//        if (withPhone){
//            fragments = new Fragment[]{
//                    FoolishBirdBindPhoneFragment.newInstance(isNextRealNameVerify()),
//                    RealnameVerifyWithPhoneFragment.newInstance(getArguments()) };
//            ViewGroup.LayoutParams params = mExViewPager.getLayoutParams();
//            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
//            mExViewPager.setLayoutParams(params);
//        }else {
            fragments = new Fragment[]{
                    FoolishBirdBindPhoneFragment.newInstance(false),
                    FoolishBirdIDVerifyFragment.newInstance(getArguments()) };
//        }


        mPagerAdapter = new LoginBindPagerAdapter(getChildFragmentManager(), Arrays.asList(fragments));
        mExViewPager.setAdapter(mPagerAdapter);

        onInitData();
        return contentView;
    }

    public void setCurrentItem(int position) {
        mExViewPager.setCurrentItem(position, true);
    }

//    PopupConfig getPopupConfig() {
//        Bundle bundle = getArguments();
//        if (bundle != null) {
//            return bundle.getParcelable("PopupConfig");
//        }
//        return null;
//    }

    private void onInitData() {
        Boolean isBindPhone = getArguments().getBoolean("Bundle_isBindPhone");
        if(isBindPhone){
            mExViewPager.setCurrentItem(0, true);
        }else{
            mExViewPager.setCurrentItem(1, true);
        }
//        if (popupConfig != null) {
//
//            if (popupConfig.isPhonePopup()) {
//                mExViewPager.setCurrentItem(0, true);
//                return;
//            }
//
//            if (popupConfig.isIdcardPopup()) {
//                mExViewPager.setCurrentItem(1, true);
//                return;
//            }
//        }
//        mExViewPager.setCurrentItem(0, true);
    }



    @Override
    public void onClick(View view) {
//        if (view == mCloseWindowBtn) {
//
//            if (mExViewPager.getCurrentItem()==1&&mBroadcastReceiver != null && !mBroadcastReceiver.isIdcardPopupCloseEnable()) {
//                Toast.makeText(getContext(), getStringIdentifier("hnssn_certifies_toast_message"), Toast.LENGTH_LONG).show();
//                return;
//            }
//
//            finishActivity();
//
//        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        WL.d("LoginBindDialogFragment", "onActivityResult ======> requestCode: " + requestCode);
    }

    private void finishActivity() {
        OnRealNameVerifyListener listener = getOnRealNameVerifyListener();
        if (listener != null) {
            listener.doFinishActivity(this);
        }
    }

    private OnRealNameVerifyListener getOnRealNameVerifyListener() {
        if (!isNullContext()) {
            Activity activity = getActivity();
            if (activity instanceof OnRealNameVerifyListener) {
                return (OnRealNameVerifyListener) activity;
            }
        }
        return null;
    }


    public interface OnRealNameVerifyListener extends OnFinishActivityListener {
        void doBindOrRealNameVerify(String cellphoneNumber, String verify, String realname, String idCard);
    }


    private class ForceRealNameCertifiesBroadcastReceiver extends BroadcastReceiver {

        private boolean isIdcardPopupCloseEnable;

        public ForceRealNameCertifiesBroadcastReceiver() {
            this.isIdcardPopupCloseEnable = true;
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null && intent.hasExtra("isIdcardPopupCloseEnable")) {
                this.isIdcardPopupCloseEnable = intent.getBooleanExtra("isIdcardPopupCloseEnable", true);
                WL.i(TAG, "onReceive ========> Value of the intent is " + this.isIdcardPopupCloseEnable);
            }
        }

        public boolean isIdcardPopupCloseEnable() {
            return isIdcardPopupCloseEnable;
        }
    }
}
